package 二链表;

/**
 * @Author XuZhuHong
 * @CreateTime 2021/9/10 16:57
 * 双链表
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(3, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(1, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(5, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(7, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        //添加节点有序
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero4);
        System.out.println("===========添加完成数据如下============");
        doubleLinkedList.list();

//        System.out.println("===========删除完成数据如下============");
//        doubleLinkedList.delete(7);
//        doubleLinkedList.list();

        System.out.println("===========修改完成数据如下============");
        HeroNode2 hero5 = new HeroNode2(7, "林冲1", "豹子头1");
        doubleLinkedList.update(hero5);
        doubleLinkedList.list();
    }

}

class DoubleLinkedList {

    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    //添加  根据序号排序
    public void addByOrder(HeroNode2 node) {
        //如果是个空链表直接添加
        if (head.next == null) {
            head.next = node;
            node.pre = head;
            return;
        }

        //遍历到最后
        HeroNode2 temp = head;
        boolean flag = false;
        while (temp.next != null) {
            if (temp.next.no == node.no) {
                System.out.println("已经存在");
                return;
            }
            if (temp.next.no > node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }


        //修改当前节点的下一个节点
        if (flag) {
            node.next = temp.next;
            temp.next = node;
            node.pre = temp;

        } else {
            temp.next = node;
            node.pre = temp;
        }

    }


    //添加
    public void add(HeroNode2 node) {

        //如果是个空链表直接添加
        if (head.next == null) {
            head.next = node;
            node.pre = head;
            return;
        }

        //遍历到最后
        HeroNode2 temp = head.next;
        while (temp.next != null) {
            temp = temp.next;
        }
        //修改当前节点的下一个节点
        temp.next = node;
        node.pre = temp;
    }

    //删除
    public void delete(int no) {
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (temp != null) {
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            temp.pre.next = temp.next;
            //如果是最后一个节点  那么就不执行指向头的节点
            if (temp.next != null)
                temp.next.pre = temp.pre;
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

    }
    //修改

    public void update(HeroNode2 node) {
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (temp != null) {
            if (temp.no == node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            temp.name = node.name;
            temp.nickname = node.nickname;
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }

    }

    //查找（遍历）
    public void list() {
        HeroNode2 temp = head.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }

}

class HeroNode2 {
    public int no;
    /**
     * 的名字
     */
    public String name;
    public String nickname;
    public HeroNode2 next; //指向下一个节点
    public HeroNode2 pre; //指向上一个节点

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}